package _22define;

import java.io.*;
import java.util.Arrays ;

public class SortAndAppendFile2 {
    public static void main(String[] args) {
        // 定义原始乱序的字母字符串
        String original = "rsztuvijxypbmnocklwaefghdq";

        // 读取文件内容
        try (FileInputStream fis = new FileInputStream("input.txt ")) {
            InputStreamReader reader = new InputStreamReader(fis, "UTF-8");
            char[] buffer = new char[1024];
            int count = reader.read (buffer);
            if (count > 0) {
                char[] characters = new char[count];
                System.arraycopy (buffer, 0, characters, 0, count);
                Arrays.sort (characters); // 对字符数组进行排序

                // 将排序后的字符数组写回文件
                try (FileOutputStream fos = new FileOutputStream("input.txt ", true)) {
                    OutputStreamWriter writer = new OutputStreamWriter(fos, "UTF-8");
                    writer.write (new String(characters)); // 追加写入
                    writer.write ("\n"); // 换行
                    writer.close ();
                }
            }
        } catch (IOException e) {
            e.printStackTrace ();
        }
    }
}
